package com.oly.cms.general.controller.api;

import java.util.ArrayList;
import java.util.List;

import com.oly.cms.common.model.support.PageData;
import com.oly.cms.general.model.po.WebCat;
import com.oly.cms.general.service.cache.WebCategoryCacheService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("/api/category")
public class CategoryApiController extends BaseController {

  @Autowired
  private WebCategoryCacheService categoryService;

  /**
   * 获取分类信息
   * 
   * @param catId
   * @return
   */
  @GetMapping("/get/{catId}")
  public AjaxResult getCatById(@PathVariable("catId") Long catId) {
    return AjaxResult.success(categoryService.getWebCatByCatId(catId));
  }

  /**
   * 通过类型获取分类列表
   * 
   * @param type 类型
   * @param id
   * @return
   */
  @GetMapping("/list/type/{type}/{id}")
  public AjaxResult listWebCatByType(@PathVariable("type") Integer type, @PathVariable("id") Long id) {
    return AjaxResult.success(categoryService.listWebCatsByType(type, id));
  }

  @GetMapping("/list/visible/{visible}/{id}")
  public AjaxResult listWebCatByVisible(@PathVariable("visible") Integer visible, @PathVariable("id") Long id) {
    return AjaxResult.success(categoryService.listWebCatsByVisible(visible, id));
  }

  @GetMapping({ "/list/{id}/themeName/{themeName}", "/list/{id}" })
  public AjaxResult listWebCatById(@PathVariable("id") Long id,
      @PathVariable(value = "themeName", required = false) String themeName) {
    return AjaxResult.success(categoryService.listWebCatsById(id, themeName));
  }

  @GetMapping({ "/tree/{catId}/themeName/{themeName}", "/tree/{catId}" })
  public AjaxResult getCatTreeById(@PathVariable("catId") Long catId,
      @PathVariable(value = "themeName", required = false) String themeName) {

    return AjaxResult.success(categoryService.listWebCatsTreeById(catId, themeName));
  }

  @GetMapping("/list")
  public AjaxResult listCats(WebCat webCat) {
    List<WebCat> list = new ArrayList<>();
    startDefaultPage();
    list = categoryService.listWebCats(webCat);
    PageData pageOne = PageData.getData(list);
    return AjaxResult.success(pageOne);
  }

  @GetMapping("/tree")
  public AjaxResult getCatTree(WebCat webCat) {
    return AjaxResult.success(categoryService.listWebCatsTree(webCat));
  }

}